/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.interfaces;

import goodenglish.application.SubtImportService;
import goodenglish.application.impl.SubtImportServiceImpl;
import goodenglish.domain.model.subt.Subt;
import goodenglish.domain.service.ServiceException;
import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.SubtFile;

import java.io.File;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;

/**
 * 字幕导入面板
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class SubtImportPane extends VBox {

	// 单词flowpanel坐标
	private File file = null;
	private Label tipLabel = new Label();
	// 显示提示信息
	private Label reulstLabel;
	private Font font = new Font(20);
	private Subt subt;

	public SubtImportPane() {
		BorderPane.setAlignment(this, Pos.TOP_LEFT);
		this.getChildren().add(getOperateButtonsLine1());
		this.getChildren().add(getLine2());

	}

	private Pane getOperateButtonsLine1() {
		// 控件缝隙
		int interspace = 10;
		/**
		 * 添加顶部控件组
		 */
		FlowPane topFlowPane = new FlowPane(interspace, interspace);
		topFlowPane.setMinWidth(Config.SCREEN_WIDTH);
		topFlowPane.setLayoutY(Config.MENU_HEIGHT);

		// 添加文件名显示框
		final TextField filePathText = new TextField("请选择字幕文件");
		filePathText.setMinSize(200, 20);
		filePathText.setEditable(false);
		topFlowPane.getChildren().add(filePathText);

		// 添加选择字幕文件按钮
		Button fileSelectbtn = new Button();
		fileSelectbtn.setText("选择字幕文件");

		fileSelectbtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				file = new FileChooser().showOpenDialog(null);
				if (subtFile.isSuportImportFile(file)) {
					filePathText.setText(file.getAbsolutePath());
					reulstLabel.setText("");
				} else {
					reulstLabel.setText("请选择txt或srt字幕文件！");
					file = null;

				}

			}
		});
		topFlowPane.getChildren().add(fileSelectbtn);

		/**
		 * 添加字幕分析按钮
		 */
		addAnalyseSubtBtn(topFlowPane);

		/**
		 * 导入信息提示框
		 */
		reulstLabel = new Label();
		reulstLabel.setTextFill(Color.RED);
		topFlowPane.getChildren().add(reulstLabel);
		return topFlowPane;
	}

	private HBox getLine2() {
		HBox hbox = new HBox();
		/**
		 * 添加提示信息显示
		 */
		ScrollPane scrollPane2 = new ScrollPane();
		scrollPane2.setMinWidth(Config.SCREEN_WIDTH);
		scrollPane2.setMinHeight(Config.SCREEN_HEIGHT - 100);
		tipLabel.setMinWidth(Config.SCREEN_WIDTH);
		tipLabel.setMaxWidth(Config.SCREEN_WIDTH);
		tipLabel.setMinHeight(Config.SCREEN_HEIGHT - 100);
		tipLabel.setWrapText(true);
		tipLabel.setFont(new Font(20));
		scrollPane2.setContent(tipLabel);

		hbox.getChildren().add(scrollPane2);

		return hbox;
	}

	/**
	 * 添加分析字幕按钮
	 */
	private void addAnalyseSubtBtn(FlowPane topFlowPane) {

		Button analyseBtn = new Button("导入字幕");
		analyseBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				// 判断是否已经导入
				if (null != file) {

					try {
						subt = subtService.importSubtAndLexicon(file);
						// 显示分析结果
						tipLabel.setText(subt.getInfoView());
						Main.instance.showModalMessage(new MessageDialog("导入已成功！"));
						// 如果字幕没有导入，则导入字幕
						reulstLabel.setText("");
					} catch (ServiceException e) {
						Main.instance.showModalMessage(new MessageDialog(e.getMessage()));
					}

				} else {
					reulstLabel.setText("请先选择文件");
					return;
				}

			}
		});
		topFlowPane.getChildren().add(analyseBtn);
	}

	private SubtImportService subtService = BeanFactory.getSingleInstance(SubtImportServiceImpl.class);

	private SubtFile subtFile = BeanFactory.getSingleInstance(SubtFile.class);
}
